<?php

namespace App\Http\Controllers\Admin;

use App\Admin\Banner;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

class BannerController extends Controller
{
    //get Admin/user 全部用户列表
    public function index(Request $request)
    {
        $banner = Banner::select()->get();
        return view('admin.banner.banner',compact('banner'));
    }
    //get Admin/banner/{banner}/edit   编辑图片信息
    public function edit($id)
    {
        $banner = Banner::find($id);
        return view('admin/banner/banedit',compact('banner'));

    }
    //    相片排序
    public function changeOrder(Request $request)
    {
        $input = $request->all();
        $album = Banner::find($input['id']);
        $album->order = $input['order'];
        $res = $album->save();
        if($res){
            $data = ['status'=>0,
                'msg'=>'图片排序更新成功',

            ];
        }else{
            $data = [
                'status'=>1,
                'msg'=>'图片排序更新失败，请重试',
            ];
        }
        return $data;
    }
    //图片修改
    public function changedit(Request $request)
    {
        $id = $request->id;
        $input = Input::except('_token');
        $input['ctime']=time();
//        return $input;
//        $rules = [
//            'name'=>'required',
//            'path'=>'required'
//        ];
//        $message = [
//            'name.required'=>'角色名不能为空!',
//            'path.required'=>'密码不能为空'
//        ];
//        $this->validate($request,$rules,$message);
        $res = Banner::where('id',$id)->update($input);
        if($res){
            $data = ['status'=>0,
                'msg'=>'照片更新成功',
            ];
        }else{
            $data = [
                'status'=>1,
                'msg'=>'照片更新失败，请重试',
            ];
        }
        return $data;

    }
    //delete Admin/banner/{banner}  删除用户信息
    public function destroy($id)
    {
        $res = Banner::where('id',$id)->select()->get();
        $res = $res[0]->path;
        $str = str_replace('/','\\',$res);
        $res = Banner::where('id',$id)->delete();
            if($res){
                @unlink(public_path().$str);
                $data = [
                    'status'=>0,
                    'msg'=>'相册删除成功!',
                ];
            }else{
                $data = [
                    'status'=>1,
                    'msg'=>'相册删除失败!',
                ];
                       }
            return $data;
        }
}
